package ru.mipt.testload.util;

import javax.swing.text.DefaultFormatter;
import javax.swing.text.MaskFormatter;
import java.text.ParseException;

/**
 * @author Pavel G. Belevich
 *         Date: 18.06.2010
 *         Time: 0:55:54
 */
public class StringFormatter extends DefaultFormatter {

    private Integer minLength;
    private Integer maxLength;

    public StringFormatter() {
    }

    @Override
    public Object stringToValue(String string) throws ParseException {
        Object result = super.stringToValue(string);
        int length = string.length();
        if (minLength != null) {
            if (length < minLength) {
                throw new ParseException("", 0);
            }
        }
        if (maxLength != null) {
            if (length > maxLength) {
                throw new ParseException("", 0);
            }
        }
        return result;
    }

    public Integer getMinLength() {
        return minLength;
    }

    public void setMinLength(Integer minLength) {
        this.minLength = minLength;
    }

    public Integer getMaxLength() {
        return maxLength;
    }

    public void setMaxLength(Integer maxLength) {
        this.maxLength = maxLength;
    }
}
